package com.example.mykotlinapp.test

/**
 *
 *反转一个单链表。

示例:

输入: 1->2->3->4->5->NULL
输出: 5->4->3->2->1->NULL
 * @author zhudeshuai
 * @since 2019/8/7
 */
class ListNode(val `val`: Int, var next: ListNode? = null)

fun reverseList(head: ListNode?): ListNode? {
    var result: ListNode? = null
    var target: ListNode? = head
    while (target!= null) {
        val tepResult = ListNode(target.`val`)
        tepResult.next = result
        result = tepResult
        target = target.next
    }
    return result
}

fun main() {
    val source = ListNode(
        1,
        ListNode(
            2,
            ListNode(
                3,
                ListNode(4, ListNode(5))
            )
        )
    )
    val result = reverseList(source)
    print(result)
}


